package com.homework;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

/**
 * @author 于振晗
 * @title: HomeWork02
 * @description: TODO 删除一条数据
 * @date 2020年09月08日
 * @path com.homework
 */
public class HomeWork0 {
    public static void main(String[] args) throws DocumentException, IOException {
        File file = new File("Java_Xml/homework/clazs.xml");
        Document read = new SAXReader().read(file);
        Element rootElement = read.getRootElement();
        List<Element> elements = rootElement.elements();
        for (int i = 0; i < elements.size(); i++) {
            Element element = elements.get(i);
            Attribute id = element.attribute("id");
            if (id.getValue().equals("08002")) {
                elements.remove(element);
            }
        }
        XMLWriter xmlWriter = new XMLWriter(new FileWriter(file), new OutputFormat().createPrettyPrint());
        xmlWriter.write(read);
        xmlWriter.close();

    }
}